home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / BCCollec / Structs / Sets / BCSet.cpp next >
Encoding:
Text File  |  1994-04-21  |  6.5 KB  |  288 lines  |  [TEXT/MPS ]

  1. //  The C++ Booch Components (Version 2.1)
  2. //  (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
  3. //
  4. //  Restricted Rights Legend
  5. //  Use, duplication, or disclosure is subject to restrictions as set forth 
  6. //  in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer 
  7. //  Software clause at DFARS 252.227-7013. 
  8. //
  9. //  BCSet.cpp
  10. //
  11. //  This file contains the definitions for the set abstract base class
  12. //  and its iterators.
  13.  
  14. #include "BCSet.h"
  15.  
  16. template<class Item>
  17. BC_TSet<Item>::BC_TSet() {}
  18.  
  19. template<class Item>
  20. BC_TSet<Item>::BC_TSet(const BC_TSet<Item>&) {}
  21.  
  22. template<class Item>
  23. BC_TSet<Item>::~BC_TSet() {}
  24.  
  25. template<class Item>
  26. BC_TSet<Item>& BC_TSet<Item>::operator=(const BC_TSet<Item>& s)
  27. {
  28.   if (this == &s)
  29.     return *this;
  30.   ((BC_TSet<Item>&)s).Lock();
  31.   Purge();
  32.   BC_TSetActiveIterator<Item> iter(s);
  33.   while (!iter.IsDone()) {
  34.     Attach(*iter.CurrentItem());
  35.     iter.Next();
  36.   }
  37.   ((BC_TSet<Item>&)s).Unlock();
  38.   return *this;
  39.  
  40. template<class Item>
  41. BC_Boolean BC_TSet<Item>::operator==(const BC_TSet<Item>& s) const
  42. {
  43.   if (this == &s)
  44.     return 1;
  45.   ((BC_TSet<Item>&)s).Lock();
  46.   if (Cardinality() != s.Cardinality()) {
  47.     ((BC_TSet<Item>&)s).Unlock();
  48.     return 0;
  49.   }
  50.   BC_TSetActiveIterator<Item> iter(*this);
  51.   while (!iter.IsDone()) {
  52.     if (!s.Exists(*iter.CurrentItem())) {
  53.       ((BC_TSet<Item>&)s).Unlock();
  54.       return 0;
  55.     }
  56.     iter.Next();
  57.   }
  58.   ((BC_TSet<Item>&)s).Unlock();
  59.   return 1;
  60.  
  61. template<class Item>
  62. BC_Boolean BC_TSet<Item>::operator!=(const BC_TSet<Item>& s) const
  63. {
  64.   return !operator==(s);
  65. }
  66.  
  67. template<class Item>
  68. void BC_TSet<Item>::Union(const BC_TSet<Item>& s)
  69. {
  70.   if (this != &s) {
  71.     ((BC_TSet<Item>&)s).Lock();
  72.     BC_TSetActiveIterator<Item> iter(s);
  73.     while (!iter.IsDone()) {
  74.       Attach(*iter.CurrentItem());
  75.       iter.Next();
  76.     }
  77.     ((BC_TSet<Item>&)s).Unlock();
  78.   }
  79. }
  80.  
  81. template<class Item>
  82. void BC_TSet<Item>::Intersection(const BC_TSet<Item>& s)
  83. {
  84.   if (this != &s) {
  85.     ((BC_TSet<Item>&)s).Lock();
  86.     BC_TSetActiveIterator<Item> iter(*this);
  87.     while (!iter.IsDone()) {
  88.       if (!s.Exists(*iter.CurrentItem()))
  89.         Detach(*iter.CurrentItem());
  90.       else
  91.         iter.Next();
  92.     }
  93.     ((BC_TSet<Item>&)s).Unlock();
  94.   }
  95. }
  96.  
  97. template<class Item>
  98. void BC_TSet<Item>::Difference(const BC_TSet<Item>& s)
  99. {
  100.   if (this == &s)
  101.     Purge();
  102.   else {
  103.     ((BC_TSet<Item>&)s).Lock();
  104.     BC_TSetActiveIterator<Item> iter(s);
  105.     while (!iter.IsDone()) {
  106.       Detach(*iter.CurrentItem());
  107.       iter.Next();
  108.     }
  109.     ((BC_TSet<Item>&)s).Unlock();
  110.   }
  111. }
  112.  
  113. template<class Item>
  114. BC_Boolean BC_TSet<Item>::IsSubset(const BC_TSet<Item>& s) const
  115. {
  116.   if (this == &s)
  117.     return 1;
  118.   ((BC_TSet<Item>&)s).Lock();
  119.   if (Cardinality() > s.Cardinality()) {
  120.     ((BC_TSet<Item>&)s).Unlock();
  121.     return 0;
  122.   }
  123.   BC_TSetActiveIterator<Item> iter(*this);
  124.   while (!iter.IsDone()) {
  125.     if (!s.Exists(*iter.CurrentItem())) {
  126.       ((BC_TSet<Item>&)s).Unlock();
  127.       return 0;
  128.     }
  129.     iter.Next();
  130.   }
  131.   ((BC_TSet<Item>&)s).Unlock();
  132.   return 1;
  133. }
  134.  
  135. template<class Item>
  136. BC_Boolean BC_TSet<Item>::IsProperSubset(const BC_TSet<Item>& s) const
  137. {
  138.   if (this == &s)
  139.     return 0;
  140.   ((BC_TSet<Item>&)s).Lock();
  141.   if (Cardinality() >= s.Cardinality()) {
  142.     ((BC_TSet<Item>&)s).Unlock();
  143.     return 0;
  144.   }
  145.   BC_TSetActiveIterator<Item> iter(*this);
  146.   while (!iter.IsDone()) {
  147.     if (!s.Exists(*iter.CurrentItem())) {
  148.       ((BC_TSet<Item>&)s).Unlock();
  149.       return 0;
  150.     }
  151.     iter.Next();
  152.   }
  153.   ((BC_TSet<Item>&)s).Unlock();
  154.   return 1;
  155. }
  156.  
  157. template<class Item>
  158. BC_Boolean BC_TSet<Item>::Attach(const Item& item, const BC_Boolean&)
  159. {
  160.   return Attach(item);
  161. }
  162.  
  163. template<class Item>
  164. void BC_TSet<Item>::Lock() {}
  165.  
  166. template<class Item>
  167. void BC_TSet<Item>::Unlock() {}
  168.  
  169. template<class Item>
  170. BC_TSetActiveIterator<Item>::BC_TSetActiveIterator(const BC_TSet<Item>& s)
  171.   : fSet(s),
  172.     fBucketIndex(s.Cardinality() ? 0 : -1),
  173.     fIndex(-1)
  174. {
  175.   if (fBucketIndex == 0) {
  176.     for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
  177.       fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
  178.       if (fIndex != -1)
  179.         return;
  180.     }
  181.   }
  182. }
  183.   
  184. template<class Item>
  185. BC_TSetActiveIterator<Item>::~BC_TSetActiveIterator() {}
  186.   
  187. template<class Item>
  188. void BC_TSetActiveIterator<Item>::Reset()
  189. {
  190.   fBucketIndex = fSet.Cardinality() ? 0 : -1;
  191.   fIndex = -1;
  192.   if (fBucketIndex == 0) {
  193.     for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
  194.       fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
  195.       if (fIndex != -1)
  196.         return;
  197.     }
  198.   }
  199. }
  200.  
  201. template<class Item>
  202. BC_Boolean BC_TSetActiveIterator<Item>::Next()
  203. {
  204.   if (fBucketIndex < fSet.NumberOfBuckets()) {
  205.     fIndex++;
  206.     if (fIndex >= fSet.Length(fBucketIndex)) {
  207.       fBucketIndex++;
  208.       fIndex = -1;
  209.       for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
  210.         fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
  211.         if (fIndex != -1)
  212.           break;
  213.       }
  214.     }
  215.     return !IsDone();
  216.   } else
  217.     return 0;
  218. }
  219.  
  220. template<class Item>
  221. BC_Boolean BC_TSetActiveIterator<Item>::IsDone() const
  222. {
  223.   if ((fBucketIndex < 0) || (fBucketIndex >= fSet.NumberOfBuckets()))
  224.     return 1;
  225.   else {
  226.     if (fIndex >= fSet.Length(fBucketIndex)) {
  227.       ((BC_TSetActiveIterator<Item>&)(*this)).fBucketIndex++;
  228.       ((BC_TSetActiveIterator<Item>&)(*this)).fIndex = -1;
  229.       for (; (fBucketIndex < fSet.NumberOfBuckets());
  230.            ((BC_TSetActiveIterator<Item>&)(*this)).fBucketIndex++) {
  231.         ((BC_TSetActiveIterator<Item>&)(*this)).fIndex =
  232.           fSet.Length(fBucketIndex) ? 0 : - 1;
  233.         if (fIndex != -1)
  234.           return 0;
  235.       }
  236.       return 1;
  237.     }
  238.     return 0;
  239.   }
  240. }
  241.  
  242. template<class Item>
  243. const Item* BC_TSetActiveIterator<Item>::CurrentItem() const
  244. {
  245.   return IsDone() ? 0 : &fSet.ItemAt(fBucketIndex, fIndex);
  246. }
  247.  
  248. template<class Item>
  249. Item* BC_TSetActiveIterator<Item>::CurrentItem()
  250. {
  251.   return IsDone() ? 0 : &((Item&)(fSet.ItemAt(fBucketIndex, fIndex)));
  252. }
  253.  
  254. template<class Item>
  255. BC_TSetPassiveIterator<Item>::BC_TSetPassiveIterator(const BC_TSet<Item>& s)
  256.   : fSet(s) {}
  257.   
  258. template<class Item>
  259. BC_TSetPassiveIterator<Item>::~BC_TSetPassiveIterator() {}
  260.   
  261. template<class Item>
  262. BC_Boolean BC_TSetPassiveIterator<Item>::Apply(BC_Boolean (*f)(const Item&))
  263. {
  264.   BC_TSetActiveIterator<Item> iter(fSet);
  265.   while (!iter.IsDone()) {
  266.     if (!f(*iter.CurrentItem()))
  267.       return 0;
  268.     else
  269.       iter.Next();
  270.   }
  271.   return 1;
  272. }  
  273.   
  274. template<class Item>
  275. BC_Boolean BC_TSetPassiveIterator<Item>::Apply(BC_Boolean (*f)(Item&))
  276. {
  277.   BC_TSetActiveIterator<Item> iter(fSet);
  278.   while (!iter.IsDone()) {
  279.     if (!f(*iter.CurrentItem()))
  280.       return 0;
  281.     else
  282.       iter.Next();
  283.   }
  284.   return 1;
  285. }  
  286.